/**
 * This example turns the ESP32 into a Bluetooth LE keyboard that writes the words, presses Enter, presses a media key and then Ctrl+Alt+Delete
 */
// #include "BleKeyboard.h"
#include "config.h"
#include "BleDangbei.h"
#include "mqtt.hpp"
#include <ArduinoJson.h>
#include <ArduinoHA.h>

// 抽象设备接口
HADevice device;
// 蓝牙硬件接口
BleDangbei bleDangbei(device, "DangbeiHID", "Espressif", 100);
// mqtt客户端
Mqtt mqttClient(device);

void setup()
{
  // 全局初始话
  if (!pid_config_init())
  {
    return;
  }
  // 设置唯一id，必须要 不然mqtt连接不上
  byte mac[6];
  WiFi.macAddress(mac);
  device.setUniqueId(mac, sizeof(mac));
  // 初始化设备
  bleDangbei.initDevice();
  bleDangbei.begin();
  PidConfig config = pid_config();
  // 通过配置参数创建MQTT客户端
  mqttClient.setServer(config.mqtt_server, config.mqtt_port);
  mqttClient.setUser(config.mqtt_user, config.mqtt_pass);
  // 连接MQTT服务器
  Serial.println("start mqtt");
  mqttClient.connect();
}

void loop()
{
  pid_config_check();
  mqttClient.loop();
  bleDangbei.sync();
  // Serial.printf("mqtt connected:%d\n", mqttClient.connected());
  delay(50);
}
